package publicroute

import (
	"fmt"
	"itodo/core/service/users"
	"itodo/http/routes"

	"github.com/RocksonZeta/irisx"
	"github.com/kataras/iris/v12"
)

//RouteUserApi /
func RouteIndex(party iris.Party) {

	party.Get("/", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		c.ViewLayout(iris.NoLayout)
		c.View("index.html")
	})
	party.Get("/index", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		c.View("index.html")
	})
	party.Get("/signin", func(ctx iris.Context) {
		fmt.Println("signin")
		c := ctx.(*irisx.Context)
		c.View("public/signin.html")
	})
	party.Post("/signin", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		var param users.SigninParam
		param.Email = c.CheckBody("email").NotBlank().String()
		param.Password = c.CheckBody("password").NotBlank().String()
		routes.Check(c)
		up, err := users.UserPrivates{}.Signin(param)
		if err != nil {
			panic(err)
		}
		routes.SessionSet(c, "uid", up.Uid, 3600)
		c.Ok(up)
	})
	party.Get("/signup", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		c.View("public/signup.html")
	})
}
